package _021_10_19

func decodeString(s string) string {
	n := len(s)
	stack := make([]string, 0)
	//strB := make([]string, 0)
	for i := n - 1; i >= 0; {
		if s[i] == ']' || 'a' <= s[i] && s[i] <= 'z' {
			stack = append(stack, string(s[i]))
			i--
		} else {
			str := ""
			for len(stack) != 0 && stack[len(stack)-1] != "]" {
				str += string(stack[len(stack)-1])
				stack = stack[0 : len(stack)-1]
			}
			stack = stack[0 : len(stack)-1]

			i--
			num := 0
			base := 1
			for i >= 0 && '0' <= s[i] && s[i] <= '9' {
				num += int(s[i]-'0') * base
				i--
				base *= 10
			}
			strRet := ""
			for j := 0; j < num; j++ {
				strRet += str
			}
			stack = append(stack, strRet)
		}
	}
	ret := ""
	for i := len(stack) - 1; i >= 0; i-- {
		ret += stack[i]
	}
	return ret
}
